import sys, os, csv
import wx, wx.grid
import pr

class App(wx.App):
  def __init__(self, redirect=False, filename=None):
    wx.App.__init__(self, redirect, filename)
    self.frame = wx.Frame(None, wx.ID_ANY, title="AMS Solver")
    self.panel = wx.Panel(self.frame, wx.ID_ANY)
    
    #Grid
    self.grid = wx.grid.Grid(self.panel)
    self.grid.CreateGrid(20, 6)
    gridsizer = wx.BoxSizer(wx.VERTICAL)
    gridsizer.Add(self.grid, 1, wx.EXPAND, 5)
    self.grid.SetCellValue(0,0, 'Party')
    self.grid.SetCellValue(0,1, 'Votes')
    self.grid.SetCellValue(0,2, 'Seats')
    self.grid.SetCellValue(0,3, 'Elected')
    self.grid.SetCellValue(0,4, 'Order')
    self.grid.SetCellValue(0,5, 'Difference')
    self.grid.SetRowLabelSize(0)
    self.grid.SetColLabelSize(0)
    self.grid.SetCellBackgroundColour(0,0,(0,0,0))    
    self.grid.SetCellBackgroundColour(0,1,(0,0,0))
    self.grid.SetCellBackgroundColour(0,2,(0,0,0))
    self.grid.SetCellBackgroundColour(0,3,(0,0,0))
    self.grid.SetCellBackgroundColour(0,4,(0,0,0))
    self.grid.SetCellBackgroundColour(0,5,(0,0,0))
    self.grid.SetCellTextColour(0,0,(255,255,255))
    self.grid.SetCellTextColour(0,1,(255,255,255))
    self.grid.SetCellTextColour(0,2,(255,255,255))
    self.grid.SetCellTextColour(0,3,(255,255,255))
    self.grid.SetCellTextColour(0,4,(255,255,255))
    self.grid.SetCellTextColour(0,5,(255,255,255))    
    
    #Menu
    menubar = wx.MenuBar()
    file = wx.Menu()
    #file.Append(-1, 'Quit', 'Quit App')
    quit = wx.MenuItem(file, 1, "Quit")
    file.AppendItem(quit)
    self.frame.Bind(wx.EVT_MENU, self.Quit, id=1)
    menubar.Append(file, '&File')
    self.frame.SetMenuBar(menubar)
    
    #Solver info at bottom
    electbtn = wx.Button(self.panel, wx.ID_ANY, 'Elect')
    self.Bind(wx.EVT_BUTTON, self.elect, electbtn)
    label = wx.StaticText(self.panel, wx.ID_ANY, 'Number of topup seats ')
    self.topups = wx.TextCtrl(self.panel, wx.ID_ANY, '1')
    btmsizer = wx.BoxSizer(wx.HORIZONTAL)
    btmsizer.Add(label, 0, wx.ALL, 5)
    btmsizer.Add(self.topups, 0, wx.ALL, 5)
    btmsizer.Add(electbtn, 0, wx.ALL, 5)
    
    #Main sizer
    sizer = wx.BoxSizer(wx.VERTICAL)
    sizer.Add(gridsizer, 1, wx.EXPAND, 5)
    sizer.Add(btmsizer, 0, wx.CENTER, 5)
    self.panel.SetSizer(sizer)
    sizer.Fit(self.frame)
    
    self.frame.Show()
  
  def Quit(self, event):
    self.frame.Close()
    
  #def Open(self, event):
    
    
  #Elect members
  def elect(self, thing):
  
    self.grid.SelectCol(4,True)
    self.grid.ClearSelection()
  
    try:
      t = int(self.topups.GetValue())
      if t == 0:
        print "This must be a number"
      else:
        print t
    except:
      print "Must be an integer"
    if t > 19:
      print "No more than 19 topups at present!"
      t = 19
      
    #Get party names
    i = 1
    parties = []
    while i < 20:      
      x = self.grid.GetCellValue(i,0)      
      if x == "":
        pass
      else:
        parties.append(x)
      i += 1
    print parties
    
    #Get votes
    i = 1
    votes = []
    while i < 20:
      x = self.grid.GetCellValue(i,1)
      if x == "":
        pass
      else:
        try:
          votes.append(int(x))
        except:
          print "Error - (" + str(i) + ", 1) must be an integer"
          break
      i += 1
    print votes
    
    #Get constituency seats (if any)
    i = 1
    conseats = []
    while i < 20:
      x = self.grid.GetCellValue(i,2)
      try:
        conseats.append(int(x))
      except:
        "Error - (" + str(i) + ", 1) must be an integer"
      i += 1
    print conseats
    
    #Check lengths
    if len(parties) != len(votes):
      print "Error! Not the same lengths."
    else:
      if len(parties) != len(conseats):
        print "Error! Not the same lengths."
      else:
        pass
        
    #Create party classes
    i = 0
    partyclass = []
    while i < 20:
      try:
        partyclass.append(pr.party(parties[i], votes[i], conseats[i]))
      except:
        print "Reached end at " + str(i)
        break
      i+=1

    #Elect members
    elected = pr.dhondt(partyclass, t)
    i = 1
    for x in elected:
      self.grid.SetCellValue(i, 4, elected[i-1].name)
      i+=1   
    electedlist = []
    for x in elected:
      electedlist.append(x.name)
      print x      
    print electedlist      
    partiescount = []
    for x in parties:
      partiescount.append(electedlist.count(x))      
    print partiescount
    i = 1
    while i < len(partiescount)+1:
      self.grid.SetCellValue(i,3,str(partiescount[i-1]))
      i+=1
      
    #Get proportionalities
    totalseats = t + sum(conseats)
    print totalseats    
    i = 1
    diffs = []
    while i < len(votes)+1:
      print votes[i-1]
      print sum(votes)
      print partiescount[i-1]
      print conseats[i-1]
      foo = float(votes[i-1])/float(sum(votes))*100.00
      print "foo = " + str(foo)
      bar = (float(partiescount[i-1])+float(conseats[i-1]))/float(totalseats)*100.00
      print "bar = " + str(bar)
      diffs.append(foo-bar)
      self.grid.SetCellValue(i,5,str(round(foo-bar,4)))
      i += 1
      print i
    gal = pr.gallagher(diffs)
    self.grid.SetCellValue(i,5,str(round(gal,4)))
    
if __name__=='__main__':
  app = App()
  app.MainLoop()
